package com.zdb;

import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;


public class Main {
    public static void main(String[] args)   {
//        testFilter();
//        testDistinct1();
//        testMap();
//        testMap1();
//        testTrueDistinct();
//        testSorted();
//        testLimit();
//        testSkip();
//        peek();
        testSupplier();
    }
    private static   List<User> getUserList() {
        List<User> userList = new ArrayList<>();

        userList.add(new User(1,"张三",18,"上海"));
        userList.add(new User(2,"王五",16,"上海"));
        userList.add(new User(3,"李四",20,"上海"));
        userList.add(new User(4,"张雷",22,"北京"));
        userList.add(new User(5,"张超",15,"深圳"));
        userList.add(new User(6,"李雷",24,"北京"));
        userList.add(new User(7,"王爷",21,"上海"));
        userList.add(new User(8,"张三丰",18,"广州"));
        userList.add(new User(9,"赵六",16,"广州"));
        userList.add(new User(10,"赵无极",26,"深圳"));

        return userList;
    }
    private static void testFilter(){
        List<User> userList = getUserList();
        List<User> collect = userList.stream().filter(user -> user.getId() > 5).collect(Collectors.toList());
        collect.forEach(System.out::println);
    }
    public static  void testDistinct(){

        List<User> list = new ArrayList<User>() {{
            add(new User(1,"Tony", 20, "12"));
            add(new User(2,"Pepper", 20, "123"));
            add(new User(3,"Tony", 22, "1234"));
            add(new User(4,"Tony", 22, "12345"));
        }};
        ArrayList<User> collect = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getName))), ArrayList::new));
        collect.forEach(System.out::println);
    }
    public static  void testDistinct1(){
        List<User> list = new ArrayList<User>() {{
            add(new User(1,"Tony", 20, "12"));
            add(new User(2,"Pepper", 20, "123"));
            add(new User(3,"Tony", 22, "1234"));
            add(new User(4,"Tony", 22, "12345"));
        }};
        ArrayList<User> collect = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getAge))), ArrayList::new));
        collect.forEach(System.out::println);
    }
    public static void testMap(){
        List<User> list = new ArrayList<User>() {{
            add(new User(1,"Tony", 20, "12"));
            add(new User(2,"Pepper", 20, "123"));
            add(new User(3,"Tony", 22, "1234"));
            add(new User(4,"Tony", 22, "12345"));
        }};
        List<String> collect = list.stream().map(user -> user.getName() + "用户").collect(Collectors.toList());
        collect.forEach(System.out::println);
    }

    public static void testMap1(){
        List<User> list = new ArrayList<User>() {{
            add(new User(1,"Tony", 20, "12"));
            add(new User(2,"Pepper", 20, "123"));
            add(new User(3,"Tony", 22, "1234"));
            add(new User(4,"Tony", 22, "12345"));
        }};
        List<Integer> collect = list.stream().map(User::getAge).collect(Collectors.toList());
        collect.forEach(System.out::println);
    }

    public static void testTrueDistinct(){
        List<User> list = new ArrayList<User>() {{
            add(new User(1,"Tony", 20, "12"));
            add(new User(2,"Pepper", 20, "123"));
            add(new User(3,"Tony", 22, "1234"));
            add(new User(4,"Tony", 22, "12345"));
        }};
        List<Integer> collect = list.stream().map(User::getAge).distinct().collect(Collectors.toList());
        collect.forEach(System.out::println);
    }
    public static void testSorted(){
        List<User> list = new ArrayList<User>() {{
            add(new User(1,"Tony", 20, "12"));
            add(new User(2,"Pepper", 20, "123"));
            add(new User(3,"Tony", 22, "1234"));
            add(new User(4,"Tony", 22, "12345"));
        }};
        List<User> collect = list.stream().sorted(Comparator.comparing(User::getLocation)).collect(Collectors.toList());
        collect.forEach(System.out::println);
    }

    public static void testLimit(){
        List<User> list = new ArrayList<User>() {{
            add(new User(1,"Tony", 20, "12"));
            add(new User(2,"Pepper", 20, "123"));
            add(new User(3,"Tony", 22, "1234"));
            add(new User(4,"Tony", 22, "12345"));
        }};
        List<User> collect = list.stream().limit(3).collect(Collectors.toList());
        collect.forEach(System.out::println);
    }
    public static void testSkip(){
        List<User> list = new ArrayList<User>() {{
            add(new User(1,"Tony", 20, "12"));
            add(new User(2,"Pepper", 20, "123"));
            add(new User(3,"Tony", 22, "1234"));
            add(new User(4,"Tony", 22, "12345"));
        }};
        List<User> collect = list.stream().skip(3).collect(Collectors.toList());
        collect.forEach(System.out::println);
    }
    public static  void peek(){
        List<User> list = new ArrayList<User>() {{
            add(new User(1,"Tony", 20, "12"));
            add(new User(2,"Pepper", 20, "123"));
            add(new User(3,"Tony", 22, "1234"));
            add(new User(4,"Tony", 22, "12345"));
        }};
        List<User> collect = list.stream().peek(user -> user.setName("Tony")).collect(Collectors.toList());
        collect.forEach(System.out::println);
    }
    public static void testSupplier(){
        Supplier<String> supplier =()-> {
            return "hello,world";
        };
        System.out.println(supplier.get());
        Supplier<Double> supplier1=()->Math.random();
        System.out.println(supplier1.get());
    }
}